package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbCandidateInfoBo;
import org.dromara.system.domain.bo.WbCompanyBlacklistBo;
import org.dromara.system.domain.vo.WbCompanyBlacklistVo;
import org.dromara.system.service.IWbCompanyBlacklistService;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbProvinceService;

import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbCompanyBlacklistImportListener extends AnalysisEventListener<WbCompanyBlacklistVo> implements ExcelListener<WbCompanyBlacklistVo> {

    private final IWbCompanyBlacklistService wbCompanyBlacklistService;
    private final IWbProvinceService wbProvinceService;
    private final IWbCompanyInfoService wbCompanyInfoService;

    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbCompanyBlacklistImportListener() {
        this.wbProvinceService = SpringUtils.getBean(IWbProvinceService.class);
        this.wbCompanyInfoService = SpringUtils.getBean(IWbCompanyInfoService.class);
        this.wbCompanyBlacklistService = SpringUtils.getBean(IWbCompanyBlacklistService.class);
    }

    @Override
    public void invoke(WbCompanyBlacklistVo wbCompanyBlacklistVo, AnalysisContext context) {
        try {
            if(StringUtils.isNotEmpty(wbCompanyBlacklistVo.getProvinceName())){
                Long id =  wbProvinceService.queryByName(wbCompanyBlacklistVo.getProvinceName());
                wbCompanyBlacklistVo.setProvinceId(id);
                WbCompanyBlacklistBo wbCompanyBlacklistBo = BeanUtil.toBean(wbCompanyBlacklistVo, WbCompanyBlacklistBo.class);

                WbCompanyBlacklistBo wbCompanyBlacklistBo1 = new WbCompanyBlacklistBo();
                if (StringUtils.isNotEmpty(wbCompanyBlacklistBo.getCompanyName())){
                    wbCompanyBlacklistBo1.setCompanyName(wbCompanyBlacklistBo.getCompanyName());
                }
                if (StringUtils.isNotEmpty(wbCompanyBlacklistBo.getRemark())){
                    wbCompanyBlacklistBo1.setRemark(wbCompanyBlacklistBo.getRemark());
                }
                if (StringUtils.isNotEmpty(wbCompanyBlacklistBo.getMeasure())){
                    wbCompanyBlacklistBo1.setMeasure(wbCompanyBlacklistBo.getMeasure());
                }
                List<WbCompanyBlacklistVo> wbCompanyBlacklistVos = wbCompanyBlacklistService.queryList(wbCompanyBlacklistBo1);
                if (wbCompanyBlacklistVos.size() <= 0) {
                    wbCompanyBlacklistService.insertByBo(wbCompanyBlacklistBo);
                    successNum++;
                }else {
                    Long id1 = wbCompanyBlacklistVos.get(0).getId();
                    wbCompanyBlacklistBo.setId(id1);
                    wbCompanyBlacklistService.updateByBo(wbCompanyBlacklistBo);
                    updateNum++;
                }
                wbCompanyInfoService.handleName(wbCompanyBlacklistBo.getCompanyName());
                successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbCompanyBlacklistVo.getCompanyName()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbCompanyBlacklistVo.getCompanyName() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbCompanyBlacklistVo.getCompanyName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbCompanyBlacklistVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + "，修改共，"+ updateNum +"条， 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbCompanyBlacklistVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
